package kr.ac.jejuuniv.controller;

import kr.ac.jejuuniv.model.User;
import kr.ac.jejuuniv.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.sun.istack.internal.NotNull;

@Controller
@RequestMapping
@SessionAttributes("user")
public class LoginController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(@RequestParam String returnUrl, @RequestParam String id, @RequestParam String password, Model model) {
		User user = userService.get(id);
		if(user.getPassword().equals(id)) {
			model.addAttribute("user", user);
			return "redirect:" + returnUrl;
		} else {
			return "login";
		}
	}
	
	@RequestMapping(value="/login")
	public String login(@RequestParam String returnUrl, Model model) {
		model.addAttribute("returnUrl", returnUrl);
		return "login";
	}
}
